<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/global.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/security_mod.php");
        require_once($_SERVER["DOCUMENT_ROOT"]."/modules/permissions.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/account_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/level_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/page_navigator.php");

	SecurityMod::login("?returnurl=accounts");

	$level = $_SESSION["account"]->getLevel();
	if ($level != "Admin")
		SecurityMod::logout();

	$pageNumber = intval(v($_REQUEST, "page"));
	if ($pageNumber < 1)
		$pageNumber = 1;
        
        $scol = "";
        $sord = "";
        $sortorder = v($_REQUEST, "sort");
        if($sortorder == null){
            // Default sorting order is Name ASCENDING
            $scol = "name";
            $sord = "asc";
        }else{
            $sorting = explode("_", $sortorder);
            if(count($sorting) == 2){
                $scol = $sorting[0];
                $sord = $sorting[1];
            }else{
                // Fake it, and get the default sort order instead
                $scol = "name";
                $sord = "asc";
            }
        }
        
        $sortorder = $scol." ".$sord;
        
	function drawNavigator() {
		global $pageCount, $pageNumber, $sortorder, $scol, $sord;
                $params = "";
                if($sortorder == "name asc"){
                    $params = "";
                }else{
                    $params = "&sort=".$scol."_".$sord;
                }
		PageNavigator::draw($pageCount, $pageNumber, 33, $params, "accounts.php");
	}

	$accounts = AccountMod::getAccountList($pageNumber, $pageCount, $sortorder);

	$levels = LevelMod::getLevels();
        $chatLevels = AccountMod::getChatLevels();
        $pwd = SecurityMod::generatePassword();
?>
<html>
<head>
    <script language="JavaScript" type="text/javascript">
    function showDetail(pid){
        var leftPos = 0;
        var topPos = 0;
        if (screen) {
            leftPos = (screen.width / 2) - 275;
            topPos = (screen.height / 2) - 390;
        }
        window.open("user_details.php?pid="+pid, "_blank", "width=550,height=780,scrollbars=1,resizable=1,left=" + leftPos + ",top=" + topPos);
}
</script>
<title><?php echo(SettingsMod::PAGE_TITLE." :: Accounts"); ?></title>
<link rel="stylesheet" href="main.css">
</head>
<body>
	<h2 align="center">Account List</h2>
	<table align="center" width="1250">
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<td colspan="10"><?php drawNavigator()?></td>
			</tr>
                        <tr>
                            <th colspan="4">Pilot</th>
                            <th colspan="2">Levels</th>
                            <th colspan="4">FCC Permissions</th>
                            <th colspan="2">FCC Override</th>
                            <th colspan="6">Scripts permissions</th>
                            <th colspan="4">Account</th>
                        </tr>
			<tr>
				<th>&nbsp;</th>
                                <th><u><a href="accounts.php?sort=pid_<?php if($scol != "pid") echo ("asc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">ID</font></a></u></th>
				<th><u><a href="accounts.php?sort=name_<?php if($scol != "name") echo ("asc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Name</font></a></u></th>
                                <th><u><a href="accounts.php?sort=alliance_<?php if($scol != "alliance") echo ("asc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Alliance</font></a></u></th>
				<th><u><a href="accounts.php?sort=level_<?php if($scol != "level") echo ("asc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Security</font></a></u></th>
				<th><u><a href="accounts.php?sort=chatlevel_<?php if($scol != "chatlevel") echo ("asc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Chat</font></a></u></th>
				<th><u>Comm</u></th>
                                <th><u>Comb</u></th>
                                <th><u>Hack</u></th>
                                <th><u>Miss</u></th>
                                <th><u>View</u></th>
                                <th><u>Ban</u></th>
				<th><u><a href="accounts.php?sort=basic_<?php if($scol != "basic") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Basic</font></a></u></th>
				<th><u><a href="accounts.php?sort=advanced_<?php if($scol != "advanced") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Adv</font></a></u></th>
                                <th><u><a href="accounts.php?sort=secret_<?php if($scol != "secret") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Secret</font></a></u></th>
                                <th><u><a href="accounts.php?sort=admin_<?php if($scol != "admin") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">Admin</font></a></u></th>
                                <th><u><a href="accounts.php?sort=ffm_<?php if($scol != "ffm") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">FFM</font></a></u></th>
				<th><u><a href="accounts.php?sort=sb_<?php if($scol != "sb") echo ("desc"); elseif($sord == "asc") echo ("desc"); else echo ("asc"); ?>"><font color="black">SB</font></a></u></th>
                                <th><u>Chat</u></th>
                                <th><u>Details</u></th>
                                <th><u>Edit</u></th>
                                <th><u>Delete</u></th>
			</tr>
			<?php
				$i = 0;
				foreach ($accounts as $key=>$value):
				$i++;
                                $permissions = new Permissions($value["permissions"]);
			?>
			<tr bgcolor='#0b0b2f'>
				<td align='right' style='cursor:crosshair'><?php echo(($pageNumber - 1) * SettingsMod::PAGE_RECORDS_PER_PAGE + $i)?>.</td>
                                <td><?php echo($key); ?></td>
				<td><?php echo($value["name"]); ?></td>
                                <td><?php echo($value["alliance"]); ?></td>
				<td style="text-align: center"><?php echo($value["level"]); ?></td>
				<td style="text-align: center"><?php echo ($value["chatlevel"]); ?></td>
                                <td style="text-align: center"><?php if($permissions->has(Permissions::MODIFY_COMMENTS)) echo ("W"); else if ($permissions->has(Permissions::VIEW_COMMENTS)) echo ("R"); ; ?></td>
                                <td style="text-align: center"><?php if($permissions->has(Permissions::ADD_COMBATS)) echo ("W"); else if ($permissions->has(Permissions::VIEW_COMBATS)) echo ("R"); ; ?></td>
                                <td style="text-align: center"><?php if($permissions->has(Permissions::ADD_HACKS)) echo ("W"); else if ($permissions->has(Permissions::VIEW_HACKS)) echo ("R"); ; ?></td>
                                <td style="text-align: center"><?php if($permissions->has(Permissions::ADD_MISSIONS)) echo ("W"); else if ($permissions->has(Permissions::VIEW_MISSIONS)) echo ("R"); ; ?></td>
                                <td style="text-align: center"><?php if($permissions->is_viewonly()) echo ("Y");  ?></td>
                                <td style="text-align: center"><?php if($permissions->is_banned()) echo ("Y");  ?></td>
                                <td style="text-align: center"><?php echo ($value["basic"]); ?></td>
                                <td style="text-align: center"><?php echo ($value["advanced"]); ?></td>
                                <td style="text-align: center"><?php echo ($value["secret"]); ?></td>
                                <td style="text-align: center"><?php echo ($value["admin"]); ?></td>
                                <td style="text-align: center"><?php echo ($value["ffm"]); ?></td>
                                <td style="text-align: center"><?php echo ($value["sb"]); ?></td>
                                
				<!-- Channel access -->
				<form method="post" action="account_channels.php">
				<td style="text-align: center">
                                    <input type="hidden" name="id" value="<?php echo($value["id"]); ?>" />
                                    <input type="submit" value="Channels" />
				</td>
				</form>

                                <!-- Details -->
                                <td style="text-align: center">
                                    <input type="button" onClick="showDetail(<?php echo($value["id"]); ?>)" value="Details" />
                                </td>
				<!-- Edit account -->
				<form method="post" action="account_edit.php">
				<td style="text-align: center">
					<input type="hidden" name="id" value="<?php echo($value["id"]); ?>" />
					<input type="submit" value="Edit" />
				</td>
				</form>
				<!-- Delete -->
				<form action="account_delete.php" method="get">
				<td style="text-align: center">
                                                <input type="hidden" name="id" value="<?php echo($value["id"]); ?>" />
						<input type="submit" value="Delete" />
				</td>
				</form>
			</tr>
			<?php endforeach; ?>
			<tr>
				<td colspan="99"><?php drawNavigator()?></td>
			</tr>
		</td>
	</tr>
	</table>

	<br />
	<h2 align="center">Create Account</h2>
	<form method="post" action="account_add.php">
            <table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center">
                <tr>
                        <th>Username:</th>
                        <td><input type="text" name="name" style="width: 100%" /></td>
                </tr>
                <tr>
                        <th>Pilot ID:</th>
                        <td><input type="text" name="pid" style="width: 100%" /></td>
                </tr>
                <tr>
                        <th>Alliance:</th>
                        <td><input type="text" name="alliance" style="width: 100%" /></td>
                </tr>
                <tr>
                        <th>Password:</th>
                        <td><input type="text" name="password" style="width: 100%" value="<?php print($pwd); ?>" /></td>
                </tr>
                <tr>
                        <th>Password Confirm:</th>
                        <td><input type="text" name="password_confirm" style="width: 100%" value="<?php print($pwd); ?>" /></td>
                </tr>
                <tr>
                        <th>Validation:</th>
                        <td><input type="text" name="validation" style="width: 100%" value="<?php print(uniqid('', true)); ?>" /></td>
                </tr>
                <tr>
                        <th>Chat Level:</th>
                        <td>
                        <select name="chatLevel" style="width: 100%">
                        <?php foreach($chatLevels as $key=>$value): ?>
                                <option value="<?php echo($key); ?>"><?php echo($chatLevels[$key]['Name']); ?></option>
                        <?php endforeach; ?>
                        </select>
                        </td>
                </tr>
                <tr>
                        <th>Security Level:</th>
                        <td>
                        <select name="level" style="width: 100%">
                        <?php foreach($levels as $level): ?>
                                <option><?php echo($level->getName()); ?></option>
                        <?php endforeach; ?>
                        </select>
                        </td>
                </tr>
                <tr>
                        <th>Basic script access:</th>
                        <td><input type="checkbox" name="BASIC" checked="checked" /></td>
                </tr>
                <tr>
                        <th>Advanced script access:</th>
                        <td><input type="checkbox" name="ADVANCED" /></td>
                </tr>
                <tr>
                        <th>Admin script access:</th>
                        <td><input type="checkbox" name="ADMIN" /></td>
                </tr>
                <tr>
                        <th>PFC SB script access:</th>
                        <td><input type="checkbox" name="PFCSB" /></td>
                </tr>
                <tr>
                        <th>Secret script access:</th>
                        <td><input type="checkbox" name="SECRET" /></td>
                </tr>
                <tr>
                        <td colspan="2" align="center"><input type="submit" value="Create Account" style="width: 100%" /></td>
                </tr>
            </table>
	</form>
</body>
</html>
